package com.bw.lib_net.test_retrofit

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.lang.reflect.InvocationHandler
import java.lang.reflect.Method
import java.lang.reflect.Proxy

/**
 * @author gjm
 * @date 2025/9/17
 * @description
 */
class TestRetrofit {

    fun test() {
        val apiService = Proxy.newProxyInstance(ApiService::class.java.classLoader,
                        arrayOf(ApiService::class.java),
                        object : InvocationHandler {
                            override fun invoke(proxy: Any?, method: Method?, args: Array<out Any>?): Any? {
                                // 获取方法上的注解 get post请求
                                val myGet = method?.getAnnotation(MyGet::class.java)
                                // 获取反射获取方法参数的注解

                                //构建Okhttp的request对象发起 网络请求
                                return method?.invoke(proxy,args)
                            }
                        }
        ) as ApiService
        // 调用
        apiService.getData()
    }

    fun test2() {
        val apiService = Retrofit.Builder()
                .baseUrl("https://www.baidu.com")
//                .addCallAdapterFactory()
                .addConverterFactory(GsonConverterFactory.create())
                .build()
                .create(ApiService::class.java)
        apiService.getData()
    }
}